#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    try:
        btn=WaitforElementByName(driver,u'我的',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementById(driver,'com.wandoujia.phoenix2:id/xj',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementById(driver,'com.wandoujia.phoenix2:id/a3o',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
        tt=WaitforElementById(driver,'com.wandoujia.phoenix2:id/dw',30)
    except NoSuchElementException:
        pass
    else:
        if tt.get_attribute('text')==u'当前没有下载任务':
            driver.back()
            return

    try:
        btn=WaitforElementById(driver,'com.wandoujia.phoenix2:id/co',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementById(driver,'com.wandoujia.phoenix2:id/apy',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementById(driver,'com.wandoujia.phoenix2:id/dk',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementById(driver,'com.wandoujia.phoenix2:id/wb',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        btn.click()

    driver.back()

def clickappdown(driver):
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    try:
        btns=WaitforElementsById(driver,'com.wandoujia.phoenix2:id/ahi',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for btn in  btns:
            try:
                down_btn=WaitforElementById(btn,'com.wandoujia.phoenix2:id/g0',10)
            except:
                continue
            else:
                down_btn.click()
                waittime=20
                while waittime:
                    text=WaitforElementById(btn,'com.wandoujia.phoenix2:id/cs',5).get_attribute("text")
                    print text
                    if '0.0%' not in text:
                        down_btn.click()
                        break
                    else:
                        waittime=waittime-1
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            break





def soft(driver):
    categorys = [u'影音播放',u'系统工具',u'社交通讯',u'手机美化',u'新闻阅读',u'摄影图像',u'考试学习',u'网上购物',u'金融理财',u'生活休闲',u'旅游出行', \
                 u'健康运动',u'办公商务',u'育儿亲子',u'休闲益智',u'跑酷竞速',u'动作冒险',u'网络游戏',u'经营策略',u'飞行射击']
    try:
        btns=WaitforElementsById(driver,'com.wandoujia.phoenix2:id/g4',30)
    except:
        raise NoSuchElementException
    else:
        btns[0].click()
    try:
        btn=WaitforElementByName(driver,u'分类',30)
    except:
        raise NoSuchElementException
    else:
        btn.click()
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                btn=WaitforElementByName(driver,u"全部",'10')
            except NoSuchElementException:
                driver.back()
                continue
            else:
                btn.click()
                try:
                    clickappdown(driver)
                except NoSuchElementException:
                    continue
        driver.back()


def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION             # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.wandoujia.phoenix2'    # apk的包名
    desired_caps['appActivity'] = 'com.pp.assistant.activity.PPMainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    del_appdown_task(driver)
    soft(driver)
    driver.quit()

#run()
